package demo.xlj.yuyi.studentapp.qitabumen;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.common.CommunityStyleActivity;
import demo.xlj.yuyi.studentapp.common.FuZhongStyleActivity;
import demo.xlj.yuyi.studentapp.common.LookAnnouncementActivity;
import demo.xlj.yuyi.studentapp.common.LookMyTongZhiActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationActivity;
import demo.xlj.yuyi.studentapp.common.SendTongZhiChooseActivity;
import demo.xlj.yuyi.studentapp.common.SocialActivity;
import demo.xlj.yuyi.studentapp.common.StudentStyleActivity;
import demo.xlj.yuyi.studentapp.common.WhereSchoolActivity;
import demo.xlj.yuyi.studentapp.common.YouthLeagueNewsActivity;
import demo.xlj.yuyi.studentapp.login.StudentMeetLoginActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;

/**
 * 作者：项路俊 on 2018/4/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍: 其他部门的新界面
 */

public class QiTaActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_Qi_Ta)
    ImageView mImageViewQiTa;
    @BindView(R.id.LinearLayout_Qi_Ta_One)
    LinearLayout mLinearLayoutQiTaOne;
    @BindView(R.id.LinearLayout_Qi_Ta_Two)
    LinearLayout mLinearLayoutQiTaTwo;
    @BindView(R.id.LinearLayout_Qi_Ta_Three)
    LinearLayout mLinearLayoutQiTaThree;
    @BindView(R.id.LinearLayout_Qi_Ta_Four)
    LinearLayout mLinearLayoutQiTaFour;
    @BindView(R.id.LinearLayout_Qi_Ta_Five)
    LinearLayout mLinearLayoutQiTaFive;
    @BindView(R.id.LinearLayout_Qi_Ta_Six)
    LinearLayout mLinearLayoutQiTaSix;
    @BindView(R.id.LinearLayout_Qi_Ta_Seven)
    LinearLayout mLinearLayoutQiTaSeven;
    @BindView(R.id.LinearLayout_Qi_Ta_Eight)
    LinearLayout mLinearLayoutQiTaEight;
    @BindView(R.id.LinearLayout_Qi_Ta_Nine)
    LinearLayout mLinearLayoutQiTaNine;
    @BindView(R.id.LinearLayout_Qi_Ta_Ten)
    LinearLayout mLinearLayoutQiTaTen;
    @BindView(R.id.NavigationView_Qi_Ta)
    NavigationView mNavigationViewQiTa;
    @BindView(R.id.Drawer_Layout_Qi_Ta)
    DrawerLayout mDrawerLayoutQiTa;
    @BindView(R.id.ImageSwitcher_qi_ta_bu_men)
    ImageSwitcher mImageSwitcherQiTaBuMen;
    private int[] images = {R.drawable.lunbo4, R.drawable.lunbo5, R.drawable.lunbo6, R.drawable.lunbo7};//图片资源数组
    private int position = 0;

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    position++;
                    mImageSwitcherQiTaBuMen.setImageResource(images[position]);
                    if (position == 3) {
                        position = -1;
                    }
                    myHandler.sendEmptyMessageDelayed(11, 4000);

                    break;
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qi_ta_activity);
        ButterKnife.bind(this);
        String name = (String) SharePerferencesUtils.get(QiTaActivity.this, "name", "", 0);
        ToastUtil.success(QiTaActivity.this, "欢迎" + name + "归来！", true, true);
        initImageView();
        View view = mNavigationViewQiTa.getHeaderView(0);

        TextView textView_Name = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Name);
        TextView textView_ShenFen = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Shen_Fen);

        textView_Name.setText("用户姓名："+name);
        textView_ShenFen.setText("用户身份：学生会其他部门");
        initListener();
    }

    private void initListener() {
        if (mNavigationViewQiTa != null) {
            mNavigationViewQiTa.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();

                    if (id == R.id.nav_camera) {
                        //表示退出登录
                        ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, StudentMeetLoginActivity.class);
                        QiTaActivity.this.finish();
                    } else if (id == R.id.nav_gallery) {
                        //表示我收藏的社团
                        ActivityUtil.openActivityFromLeftToRightByResult(QiTaActivity.this, CommunityStyleActivity.class, "type", "1");
                    } else if (id == R.id.nav_slideshow) {
                        //表示我的发布
                        ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, LookMyTongZhiActivity.class);
                    }

                    return true;
                }
            });
        }

        if (mImageViewQiTa != null) {
            mImageViewQiTa.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawerLayoutQiTa.openDrawer(GravityCompat.START);
                }
            });
        }
    }


    //初始化，上面的轮播图
    private void initImageView() {
        mImageSwitcherQiTaBuMen.setFactory(new ViewSwitcher.ViewFactory() {//设定工厂，每进来一个图片都用一个ImageView接收
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(QiTaActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });//这个设置Factory是必须的
        mImageSwitcherQiTaBuMen.setImageResource(images[position]);//设置初始图片,也是通过这个代码设置在控件上显示的图片
        mImageSwitcherQiTaBuMen.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//动画淡入
        mImageSwitcherQiTaBuMen.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//动画淡出

        myHandler.sendEmptyMessageDelayed(11, 3000);

        mImageSwitcherQiTaBuMen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击事件
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, WhereSchoolActivity.class);
            }
        });
    }

    @OnClick({R.id.LinearLayout_Qi_Ta_One, R.id.LinearLayout_Qi_Ta_Two, R.id.LinearLayout_Qi_Ta_Three, R.id.LinearLayout_Qi_Ta_Four, R.id.LinearLayout_Qi_Ta_Five, R.id.LinearLayout_Qi_Ta_Six, R.id.LinearLayout_Qi_Ta_Seven, R.id.LinearLayout_Qi_Ta_Eight, R.id.LinearLayout_Qi_Ta_Nine, R.id.LinearLayout_Qi_Ta_Ten})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.LinearLayout_Qi_Ta_One:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, FuZhongStyleActivity.class);//附中风采
                break;
            case R.id.LinearLayout_Qi_Ta_Two:
                ActivityUtil.openActivityFromLeftToRightByResult(QiTaActivity.this, CommunityStyleActivity.class,"type","0");//社团风貌
                break;
            case R.id.LinearLayout_Qi_Ta_Three:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, YouthLeagueNewsActivity.class);//附中要闻
                break;
            case R.id.LinearLayout_Qi_Ta_Four:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, SocialActivity.class);//社团活动
                break;
            case R.id.LinearLayout_Qi_Ta_Five:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, StudentStyleActivity.class);//学生会风采
                break;
            case R.id.LinearLayout_Qi_Ta_Six:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, LookAnnouncementActivity.class);//公告通知
                break;
            case R.id.LinearLayout_Qi_Ta_Seven:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, SendTongZhiChooseActivity.class);//发布通知
                break;
            case R.id.LinearLayout_Qi_Ta_Eight:
                ActivityUtil.openActivityForResultFromLeftToRight(QiTaActivity.this, MyCommicationActivity.class, 99);//我的
                break;
            case R.id.LinearLayout_Qi_Ta_Nine:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, YouthLeagueNewsActivity.class);//附中要闻
                break;
            case R.id.LinearLayout_Qi_Ta_Ten:
                ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, SocialActivity.class);//社团活动
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == 88) {
            ActivityUtil.openActivityFromLeftToRight(QiTaActivity.this, StudentMeetLoginActivity.class);
            QiTaActivity.this.finish();

        }
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {

            if((System.currentTimeMillis()-exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();

            }else
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
